package logintest.servlet;

import logintest.bean.User;
import logintest.dao.UserDao;
import logintest.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/test")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //接收前台传过来的数据
        String uname = req.getParameter("uname");
        String upass = req.getParameter("upass");
        HttpSession session = req.getSession();
        session.setAttribute("uname",uname);
        User user=new User();
        user.setUname(uname);
        user.setUpass(upass);
        UserDao userDao = new UserDaoImpl();
        int test = userDao.Test(user);
        if (test>0){
            resp.sendRedirect("/success.jsp");
        }else {
            //添加cookie
            Cookie cookie = new Cookie("unameCookie",uname);
            //返回给前端,前端可以用JSP获得
            resp.addCookie(cookie);
            resp.sendRedirect("/login.jsp");
        }
    }
}
